package com.gjp.jooqspringboot.controller;

import com.gjp.jooqspringboot.bean.User;
import com.gjp.jooqspringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Gjp on 2018/7/25.
 */
@RestController
public class UserController {

	@Autowired
	UserService userService;

	@GetMapping("/insert")
	public String insert(User user) {
		try {
			user = userService.insert(user);
			return user.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	@GetMapping("/update")
	public String update(User user) {
		try {
			user = userService.update(user);
			return user.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	@GetMapping("/delete")
	public String delete(User user) {
		try {
			boolean deleted = userService.delete(user);
			return deleted + "";
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	@GetMapping("/selectById")
	public String selectById(User user) {
		try {
			return userService.selectById(user).toString();
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	@GetMapping("/selectAll")
	public String selectAll() {
		try {
			return userService.selectAll().toString();
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	@GetMapping("/selectPage")
	public String selectPage(Integer page, Integer size) {
		try {
			return userService.selectPage(page,size).toString();
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	@GetMapping("/transcationTest")
	public String transcationTest(){
		try {
			return userService.transcationTest();
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

}
